package com.lin.subject.domain.service.impl;

import com.google.common.base.Preconditions;
import com.lin.subject.commom.enums.CategoryTypeEnum;
import com.lin.subject.commom.enums.IsDeletedFlagEnum;
import com.lin.subject.domain.convert.SubjectLabelConvert;
import com.lin.subject.domain.convert.SubjectLabelConverter;
import com.lin.subject.domain.entity.SubjectLabelBO;
import com.lin.subject.domain.service.SubjectLabelDomainService;
import com.lin.subject.infra.basic.entity.SubjectCategory;
import com.lin.subject.infra.basic.entity.SubjectLabel;
import com.lin.subject.infra.basic.entity.SubjectMapping;
import com.lin.subject.infra.basic.service.SubjectCategoryService;
import com.lin.subject.infra.basic.service.SubjectLabelService;
import com.lin.subject.infra.basic.service.SubjectMappingService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Override
    public void add(SubjectLabelBO subjectLabelBo) {
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToLabel(subjectLabelBo);

        validateBaseData(subjectLabel);

        subjectLabelService.add(subjectLabel);
    }

    @Override
    public void update(SubjectLabelBO subjectLabelBo) {
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToLabel(subjectLabelBo);

        Preconditions.checkNotNull(subjectLabelBo.getId(),"Id不能为空");
        validateBaseData(subjectLabel);

        subjectLabelService.update(subjectLabel);
    }

    @Override
    public void delete(SubjectLabelBO subjectLabelBo) {
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToLabel(subjectLabelBo);
        Preconditions.checkNotNull(subjectLabelBo.getId(),"Id不能为空");

        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());

        subjectLabelService.update(subjectLabel);
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        //如果当前分类是1级分类，则查询所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
        if(CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()){
            SubjectLabel subjectLabel = new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
            List<SubjectLabel> labelList = subjectLabelService.queryByCondition(subjectLabel);
            List<SubjectLabelBO> labelResultList = SubjectLabelConverter.INSTANCE.convertLabelToBoList(labelList);
            return labelResultList;
        }
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
        if (CollectionUtils.isEmpty(mappingList)) {
            return Collections.emptyList();
        }
        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
        List<SubjectLabelBO> boList = new LinkedList<>();
        labelList.forEach(label -> {
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setCategoryId(categoryId);
            bo.setSortNum(label.getSortNum());
            boList.add(bo);
        });
        return boList;
    }

    /**
     * 根据Id查询，查询所有已被设置题目的标签列表
     * @param subjectLabelBo
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryAnswerLabelByCategoryId(SubjectLabelBO subjectLabelBo) {
        List<Long> ids = subjectMappingService.getByCategoryId(subjectLabelBo.getCategoryId()).stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());

        if (ids.size() > 0) {
            List<SubjectLabel> labelList = subjectLabelService.listByIds(ids);
            return SubjectLabelConvert.INSTANCE.convertToListBo(labelList);
        }
        return new ArrayList<>();
    }

    /**
     * 查询标签列表
     *
     * @param subjectLabelBo
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryLabel(SubjectLabelBO subjectLabelBo) {
        Preconditions.checkNotNull(subjectLabelBo.getCategoryId(), "分类ID不能为空");

        SubjectLabel subjectLabel = new SubjectLabel();
        subjectLabel.setCategoryId(subjectLabelBo.getCategoryId());
        List<SubjectLabel> list = subjectLabelService.findListByCondition(subjectLabel);
        return SubjectLabelConvert.INSTANCE.convertToListBo(list);
    }

    private void validateBaseData(SubjectLabel subjectLabel) {
        Preconditions.checkArgument(StringUtils.hasLength(subjectLabel.getLabelName()), "标签名称不能为空");
        Preconditions.checkNotNull(subjectLabel.getCategoryId(), "分类ID不能为空");
        Preconditions.checkNotNull(subjectLabel.getSortNum(), "序号不能为空");
    }
}
